package com.ruoyi.system.service.impl;


import com.ruoyi.system.domain.vo.ContractTemplateVO;
import com.ruoyi.system.dto.ThirdTemplateDTO;
import com.ruoyi.system.service.IContractTemplateService;
import com.tencentcloudapi.common.exception.TencentCloudSDKException;
import com.tencentcloudapi.essbasic.v20210526.EssbasicClient;
import com.tencentcloudapi.essbasic.v20210526.models.DescribeTemplatesRequest;
import com.tencentcloudapi.essbasic.v20210526.models.DescribeTemplatesResponse;
import com.tencentcloudapi.essbasic.v20210526.models.TemplateInfo;
import org.apache.commons.compress.utils.Lists;
import org.apache.http.client.utils.DateUtils;
import org.springframework.stereotype.Service;

import java.util.Date;
import java.util.List;

import static com.tencent.essbasic.common.CreateFlowUtils.initClient;
import static com.tencent.essbasic.common.CreateFlowUtils.setAgent;

/**
 * @ProjectName: back
 * @Package: com.ruoyi.system.service.impl
 * @ClassName: ContractTemplateServiceImpl
 * @Author: haiyue
 * @Description: 运营-合同模板管理
 * @Date: 2025/5/14 下午 2:15:25
 * @Version: 1.0
 */
@Service
public class ContractTemplateServiceImpl implements IContractTemplateService {
    @Override
    public List<ContractTemplateVO> contractTemplateList(String templateName) throws TencentCloudSDKException {
        List<ContractTemplateVO> templateVOList = Lists.newArrayList();
        ThirdTemplateDTO thirdTemplateDTO = new ThirdTemplateDTO();

        // 实例化一个client
        EssbasicClient client = initClient();
        // 实例化一个请求对象,每个接口都会对应一个request对象
        DescribeTemplatesRequest req = new DescribeTemplatesRequest();
        req.setAgent(setAgent());
        req.setLimit(thirdTemplateDTO.getLimit());
        req.setOffset(thirdTemplateDTO.getOffset());
        req.setQueryAllComponents(false);
        req.setWithPreviewUrl(true);
        req.setUserFlowTypeId(thirdTemplateDTO.getUserFlowTypeId());
        DescribeTemplatesResponse describeTemplatesResponse = client.DescribeTemplates(req);
        if(describeTemplatesResponse!=null && describeTemplatesResponse.getTemplates().length>0){
            for(int i=0;i<describeTemplatesResponse.getTemplates().length;i++){
               TemplateInfo templateInfo =  describeTemplatesResponse.getTemplates()[i];
                ContractTemplateVO vo1 = new ContractTemplateVO();
                vo1.setTemplateName(templateInfo.getTemplateName());
                vo1.setTemplateDesc(templateInfo.getDescription());
                if(templateInfo.getUserFlowType()!=null){
                    vo1.setContractType(templateInfo.getUserFlowType().getName());
                }
                vo1.setCreateTime(new Date(templateInfo.getCreatedOn()*1000));
                vo1.setTemplateLink(templateInfo.getPreviewUrl());
                templateVOList.add(vo1);
            }
        }
        describeTemplatesResponse.getTemplates();
        return templateVOList;
    }
}
